package com.qijian.maindata.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.exception.ServiceException;
import com.qijian.common.utils.StringUtils;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.maindata.domain.query.ProcessQuery;
import com.qijian.maindata.service.IProcessService;
import io.swagger.annotations.Api;
import com.qijian.maindata.domain.Process;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工序Controller
 *
 * @author ruoyi
 * @date 2021-08-19
 */
@Api(tags = "工序")
@RestController
@RequiredArgsConstructor
@RequestMapping("/business/process")
public class ProcessController extends BaseController {
    private final IProcessService processService;

    /**
     * 查询工序列表
     */
    @PreAuthorize("@ss.hasPermi('business:process:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProcessQuery query, Page<Process> page) {
        return getDataTable(processService.page(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }

    /**
     * 导出工序列表
     */
    @PreAuthorize("@ss.hasPermi('business:process:export')")
    @Log(title = "工序", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProcessQuery query) {
        return new ExcelUtil<>(Process.class).exportExcel(processService.list(WhereEntityTool.invoke(query)), "工序数据");
    }

    /**
     * 获取工序详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:process:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(processService.getProcessById(id));
    }

    /**
     * 新增工序
     */
    @PreAuthorize("@ss.hasPermi('business:process:add')")
    @Log(title = "工序", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Process process) {
        ProcessQuery query = new ProcessQuery();
        query.setType(process.getType());
        query.setName(StringUtils.trim(process.getName()));
        query.setRemark(StringUtils.trim(process.getRemark()));
        if (processService.count(WhereEntityTool.invoke(query)) > 0) {
            throw new ServiceException("E01:已存在该工序");
        }
        query.setName(null);
        query.setRemark(null);
        process.setCode("GX-" + (StringUtils.isNotEmpty(process.getType()) ? process.getType().toUpperCase() : "ALL") + "-" + String.format("%06d", processService.count(WhereEntityTool.invoke(query)) + 1));
        return toAjax(processService.save(process));
    }

    /**
     * 修改工序
     */
    @PreAuthorize("@ss.hasPermi('business:process:edit')")
    @Log(title = "工序", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Process process) {
        ProcessQuery query = new ProcessQuery();
        query.setType(process.getType());
        query.setCode(process.getCode());
        query.setName(StringUtils.trim(process.getName()));
        query.setRemark(StringUtils.trim(process.getRemark()));
        query.setFid(process.getId());
        if (processService.count(WhereEntityTool.invoke(query)) > 0) {
            throw new ServiceException("E01:已存在该工序");
        }

        return toAjax(processService.updateById(process));
    }

    /**
     * 删除工序
     */
    @PreAuthorize("@ss.hasPermi('business:process:remove')")
    @Log(title = "工序", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(processService.removeByIds(ids));
    }
}
